package br.com.webhomesites.configuracao.presenter;

import br.com.webhomesites.configuracao.contracts.IUsuarioView;
import br.com.webhomesites.configuracao.view.UsuarioView;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class UsuarioPresenterActionListeners {  
      
    static class AlterarActionListener implements ActionListener {  
        private UsuarioPresenter presenter;  
          
        public AlterarActionListener(UsuarioPresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            presenter.setOperacao(presenter.UPDATE_STRATEGY);  
            presenter.habilitarEdicao(true);  
              
            IUsuarioView view = (IUsuarioView)presenter.getView();  
              
            view.enableBtnInserir(true);  
            view.enableBtnRemover(true);
            view.enableBtnCancelar(true);  
            view.enableBtnSalvar(true); 
            view.enableBtnAlterar(true); 
     
        }  
    }  
      
    static class CancelarActionListener implements ActionListener {  
        private UsuarioPresenter presenter;  
          
        public CancelarActionListener(UsuarioPresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            
            IUsuarioView view = (IUsuarioView)presenter.getView();  
              
            view.clearFields();  
              
            presenter.habilitarEdicao(false);  
            
            view.enableBtnInserir(true);  
            view.enableBtnRemover(false);
            view.enableBtnCancelar(false);  
            view.enableBtnSalvar(false); 
            view.enableBtnAlterar(false);              

        }  
    }  
      
    static class SalvarActionListener implements ActionListener {  
        private UsuarioPresenter presenter;  
          
        public SalvarActionListener(UsuarioPresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            presenter.getOperacao().execute();
            //presenter.setOperacao(presenter.UPDATE_STRATEGY);
            presenter.habilitarEdicao(false);  
            
            //setUpViewListeners();
              
            IUsuarioView view = (IUsuarioView)presenter.getView();  
              
            view.enableBtnInserir(true);
            view.enableBtnRemover(false);
            view.enableBtnCancelar(false);  
            view.enableBtnSalvar(false); 
            view.enableBtnAlterar(false);  
                        
            System.out.println("Passou na ação salvar!");
  
        }  
    }  
      
    static class InserirActionListener implements ActionListener {  
        private UsuarioPresenter presenter; 
        
        private static int codigoUsuario = 0;
          
        public InserirActionListener(UsuarioPresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            presenter.setOperacao(presenter.INSERT_STRATEGY);              
            presenter.novoUsuario();  
              
            IUsuarioView view = (IUsuarioView)presenter.getView();  
              
            presenter.habilitarEdicao(true);  
            view.clearFields();  
            
            codigoUsuario = codigoUsuario+1;
            
            view.setCodigo(codigoUsuario);
            
            view.enableBtnInserir(false);  
            view.enableBtnRemover(false);
            view.enableBtnCancelar(true);  
            view.enableBtnSalvar(true); 
            view.enableBtnAlterar(false);
            
            System.out.println("Passou na ação inserir!");
        }  
    }  
      
    static class RemoverActionListener implements ActionListener {  
        private UsuarioPresenter presenter;  
          
        public RemoverActionListener(UsuarioPresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
              
            if (JOptionPane.showConfirmDialog((Component)presenter.getView(), "Deseja remover?", "Pergunta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {  
                  
                IUsuarioView view = (IUsuarioView)presenter.getView();  
                  
                presenter.removerUsuario();  
                view.clearFields();  
                
                view.enableBtnInserir(true);
                view.enableBtnAlterar(false);  
                view.enableBtnRemover(false);  
            }  
        }  
    }  
      
    static class SairActionListener implements ActionListener {  
        @Override
        public void actionPerformed(ActionEvent evt) {  
            System.exit(0);  
        }  
    }  
}  